package com.link.base.base.home.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.home.dao.mybatis.mapper.PageConfigMapper;
import com.link.base.base.home.model.HomePage;
import com.link.base.base.home.model.PageConfig;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: weixuan
 * created: 2019-01-12
 * comments: 首页菜单栏配置Service
 */
@Service
public class PageConfigServiceImpl extends BasicServiceImpl<PageConfig> implements PageConfigService {

    @Resource
    private PageConfigMapper pageConfigMapper;

    @Resource
    private HomePageService homePageService;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<PageConfig> getBasicMapper() {
        return pageConfigMapper;
    }

    /**
     *
     * @author weixuan
     * @created 2019-01-12
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public List<PageConfig> initUserConfig() throws Exception {
        List<PageConfig> list = new ArrayList<PageConfig>();
        CoreUser user  = UserUtil.getUser();
        Long userId = user.getId();
        /** 查询用户是否已存在自定义初始化首页菜单栏*/
        PageConfig entity = new PageConfig();
        entity.setUserId(userId);
        List<PageConfig> pageConfigList = pageConfigMapper.queryAll(entity);

        Set userPageSet = new HashSet<String>();
        for (PageConfig item : pageConfigList) {
            userPageSet.add(item.getUrlId());
        }

        /** 不存在自定义配置，系统默认初始化用户自定义首页菜单栏*/
        if (pageConfigList.size() == 0) {
            HomePage homePage = new HomePage();
            List<HomePage> homePageList = homePageService.queryAll(homePage);
            //当前账套无首页菜单，则拷贝账套000下的首页菜单设置为当前账套的首页菜单
            if (homePageList.size() < 1) {
                Long corpId = homePage.getLoginCorpId();
                homePage.setLoginCorpId(0L);
                List<HomePage> homePageTempList = homePageService.queryAll(homePage);
                for (HomePage homePagetemp : homePageTempList) {
                    homePagetemp.setId(keyGenerateService.keyGenerate());
                    homePagetemp.setLoginCorpId(corpId);
                }
                homePageService.batchInsert(homePageTempList);
                homePageList.addAll(homePageTempList);
            }
            for (HomePage item : homePageList) {
                Long urlId = item.getId();
                int sortNum = item.getSortNumber();
                PageConfig pageConfig = new PageConfig();
                Long id = keyGenerateService.keyGenerate();
                pageConfig.setId(id);
                pageConfig.setUrlId(urlId);
                pageConfig.setUserId(userId);
                pageConfig.setViewStatus("Y");
                pageConfig.setSortNumber(sortNum);
                pageConfigMapper.insert(pageConfig);
            }
        } else {
            HomePage homePage = new HomePage();
            List<HomePage> homePageList = homePageService.queryAll(homePage);
            for (HomePage item : homePageList) {
                Long urlId = item.getId();
                if (!userPageSet.contains(urlId)) {
                    int sortNum = item.getSortNumber();
                    PageConfig pageConfig = new PageConfig();
                    Long id = keyGenerateService.keyGenerate();
                    pageConfig.setId(id);
                    pageConfig.setUrlId(urlId);
                    pageConfig.setUserId(userId);
                    // 后续自定义初始化菜单栏默认不可见
                    pageConfig.setViewStatus("N");
                    pageConfig.setSortNumber(sortNum);
                    pageConfigMapper.insert(pageConfig);
                }
            }
        }
//        entity.setOrderByClause("viewStatus，sortNumber");
        entity.setPageFlag(false);
        list = pageConfigMapper.queryByExamplePage(entity);

        return list;
    }

}
